package com.example.taobaounion.ui.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.taobaounion.R
import com.example.taobaounion.model.bean.SelectCategoryBean.DataBean

/**
 *@author : Administrator
 *@descreption : 精选页面的分类recyclerview的适配器
 */
class SelectCategoryAdapter : RecyclerView.Adapter<SelectCategoryAdapter.ViewHolder>() {
    private var mDataList: List<DataBean> = ArrayList<DataBean>()
    var mSelectCategoryItemOnClickListener: SelectCategoryItemOnClickListener? = null
    var prePosition = 0

    fun setData(dataList: List<DataBean>) {
        this.mDataList = dataList
        notifyDataSetChanged()
    }

    fun setSelectCategoryItemOnClickListener(selectCategoryItemOnClickListener: SelectCategoryItemOnClickListener) {
        this.mSelectCategoryItemOnClickListener = selectCategoryItemOnClickListener
    }


    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val categoryTv: TextView = itemView.findViewById(R.id.categoryTv)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.item_select_category_text, parent, false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int {
        return this.mDataList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.categoryTv.text = mDataList[position].favorites_title
        holder.categoryTv.setOnClickListener {

            // holder.categoryTv.setBackgroundColor(holder.categoryTv.resources.getColor(R.color.colorEFEEEE))

            notifyDataSetChanged()
            prePosition = position
            mSelectCategoryItemOnClickListener?.selectCategoryItemOnClick(mDataList[position])
        }
        if (prePosition ==position){
            holder.categoryTv.setBackgroundColor(holder.categoryTv.resources.getColor(R.color.colorEFEEEE))
        }else{
            holder.categoryTv.setBackgroundColor(holder.categoryTv.resources.getColor(R.color.colorWhile))
        }

    }

    interface SelectCategoryItemOnClickListener {
        fun selectCategoryItemOnClick(dataBean: DataBean)
    }
}